﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SkiPathFinder
{
    public class OpeningForm : Form
    {
        #region Constructor

        public OpeningForm()
        {
            InitializeComponent();

            this.TopMost = true;
            btnOpenMap.Enabled = false;
            frmMain = new MainForm();
            frmMain.FormClosed += new FormClosedEventHandler(frmMain_FormClosed);

            PopulateMapList();
        }

        #endregion

        

        private Label label1;
        private Label label2;
        private ListBox lstbxAreas;
        private Label label3;
        private Button btnOpenMap;
        private Button btnNewArea;

        private MainForm frmMain;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(OpeningForm));
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.lstbxAreas = new System.Windows.Forms.ListBox();
            this.label3 = new System.Windows.Forms.Label();
            this.btnOpenMap = new System.Windows.Forms.Button();
            this.btnNewArea = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Verdana", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(13, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(161, 18);
            this.label1.TabIndex = 0;
            this.label1.Text = "SkiPathFinder 1.0";
            // 
            // label2
            // 
            this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.ForeColor = System.Drawing.Color.Blue;
            this.label2.Location = new System.Drawing.Point(216, 16);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(97, 15);
            this.label2.TabIndex = 1;
            this.label2.Text = "JiriTeva, 2010";
            // 
            // lstbxAreas
            // 
            this.lstbxAreas.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.lstbxAreas.FormattingEnabled = true;
            this.lstbxAreas.Location = new System.Drawing.Point(21, 84);
            this.lstbxAreas.Name = "lstbxAreas";
            this.lstbxAreas.Size = new System.Drawing.Size(292, 108);
            this.lstbxAreas.TabIndex = 2;
            this.lstbxAreas.SelectedIndexChanged += new System.EventHandler(this.lstbxAreas_SelectedIndexChanged);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.Location = new System.Drawing.Point(18, 60);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(201, 16);
            this.label3.TabIndex = 3;
            this.label3.Text = "Please Select the Skiing Area ";
            // 
            // btnOpenMap
            // 
            this.btnOpenMap.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnOpenMap.Location = new System.Drawing.Point(21, 210);
            this.btnOpenMap.Name = "btnOpenMap";
            this.btnOpenMap.Size = new System.Drawing.Size(109, 31);
            this.btnOpenMap.TabIndex = 4;
            this.btnOpenMap.Text = "Open Area";
            this.btnOpenMap.UseVisualStyleBackColor = true;
            this.btnOpenMap.Click += new System.EventHandler(this.btnOpenMap_Click);
            // 
            // btnNewArea
            // 
            this.btnNewArea.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnNewArea.Location = new System.Drawing.Point(142, 210);
            this.btnNewArea.Name = "btnNewArea";
            this.btnNewArea.Size = new System.Drawing.Size(171, 31);
            this.btnNewArea.TabIndex = 5;
            this.btnNewArea.Text = "Create New Skiing Area";
            this.btnNewArea.UseVisualStyleBackColor = true;
            // 
            // OpeningForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(337, 256);
            this.Controls.Add(this.btnNewArea);
            this.Controls.Add(this.btnOpenMap);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.lstbxAreas);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "OpeningForm";
            this.Text = "Welcome to the SkiPathFinder";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        /// <summary>
        /// Determines whether the form is open for the second time
        /// </summary>
        public bool SecondOpen = false;


        /// <summary>
        /// When the main form is closed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Open existing ski map
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenMap_Click(object sender, EventArgs e)
        {
            if (SecondOpen == false)
            {
                frmMain.Owner = this;
                frmMain.LoadData(Globals.SelectedAreaFolder);
                frmMain.Show();
                this.Hide();
            }
            else
            {
                DialogResult = DialogResult.OK;
            }
        }

        /// <summary>
        /// Populates the list of available skiing areas from the 'Maps' subdirectory
        /// </summary>
        private void PopulateMapList()
        {
            string mainDir = AppDomain.CurrentDomain.BaseDirectory;
            string mapDir = Path.Combine(mainDir, "maps");
            string[] subfolders = Directory.GetDirectories(mapDir);

            foreach(string skiAreaFolder in subfolders)
            {
                string[] grids = Directory.GetFiles(skiAreaFolder, "*.bgd");
                if (grids.Length > 0)
                {
                    string skiAreaName = skiAreaFolder.Substring(skiAreaFolder.LastIndexOf(Path.DirectorySeparatorChar) + 1);
                    lstbxAreas.Items.Add(skiAreaName);
                    
                }
            }
        }

        private void lstbxAreas_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstbxAreas.SelectedItems.Count > 0)
            {
                string skiAreaName = lstbxAreas.SelectedItem.ToString();
                string mapDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "maps");
                Globals.SelectedAreaFolder = Path.Combine(mapDir, skiAreaName);
                
                btnOpenMap.Enabled = true;
            }
        }

    }
}
